package com.totoro.tyrank.listener;

import com.totoro.tyrank.TyRank;
import com.totoro.tyrank.constants.InvRankTypeConst;
import com.totoro.tyrank.constants.NBTKeyConst;
import com.totoro.tyrank.inventory.AllRankInventory;
import com.totoro.tyrank.inventory.CurrentRankInventory;
import com.totoro.tyrank.inventory.HistoryRankInventory;
import com.totoro.tyrank.inventory.RankContentInventory;
import com.totoro.tyrank.utils.ItemStackUtil;
import com.totoro.tyrank.utils.NBTUtil;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class HistoryRankInventoryListener implements Listener {

    private final List<Integer> availableRawSlots = Stream.of(2,4,6,19,20,21,22,23,24,25,27,35).collect(Collectors.toList());

    private final Map<UUID, Long> coolDownMap = new HashMap<>();

    @EventHandler
    public void OnFire(InventoryClickEvent e) {
        if (e.getView().getTitle().equals(TyRank.getLanguageManager().getLanguageData().getGui().getHistoryRankInvTitle())) {
            Player player = (Player) e.getView().getPlayer();
            e.setCancelled(true);
            if (!availableRawSlots.contains(e.getRawSlot())) {
                return;
            }
            // 检查冷却
            long currentTime = System.currentTimeMillis();
            if (coolDownMap.containsKey(player.getUniqueId()) &&
                    currentTime - coolDownMap.get(player.getUniqueId()) < TyRank.getConfigManager().getConfigData().getClickCooldown()) {
                player.sendMessage(TyRank.getConfigManager().getConfigData().getPrefix() + TyRank.getLanguageManager().getLanguageData().getGui().getClickCooldown());
                return;
            }
            // 更新冷却时间
            coolDownMap.put(player.getUniqueId(), currentTime);
            
            ItemStack currentItem = e.getCurrentItem();
            // 如果点击的是上面的排行榜类型
            // 如果点击的所有排行榜
            if(e.getRawSlot() == 2) {
                AllRankInventory.openMainInv(player);
            }

            // 如果点击的当前排行榜
            if(e.getRawSlot() == 4) {
                CurrentRankInventory.openMainInv(player);
            }

            // 如果点击的是历史排行榜
            if(e.getRawSlot() == 6) {
                player.sendMessage(TyRank.getConfigManager().getConfigData().getPrefix() +
                        TyRank.getLanguageManager().getLanguageData().getMsg().getAlreadyHistoryRankInv());
                return;
            }

            // 如果点击的是中间具体的排行榜 19 - 25
            if((e.getRawSlot() >= 19 && e.getRawSlot() <= 25)) {
                // 判断点击的物品是否为空
                if(ItemStackUtil.isBlank(currentItem)) {
                    return;
                }
                // 获取当前物品的rankId- NBT
                String rankId = NBTUtil.getNBT(currentItem, NBTKeyConst.RANK_ID);
                RankContentInventory.openMainInv(player, rankId, InvRankTypeConst.HISTORY_RANK_INV);
            }



            // 获取点击类型
            ClickType clickType = e.getClick();
            // 如果点击的是上一页，判断左键还是右键
            if(e.getRawSlot() == 27) {
                if(clickType == ClickType.LEFT) {
                    HistoryRankInventory.preCurrentPage(player);
                } else if (clickType == ClickType.RIGHT) {
                    HistoryRankInventory.firstCurrentPage(player);
                }
                return;
            }
            // 如果点击的是下一页，判断左键还是右键
            if(e.getRawSlot() == 35) {
                if(clickType == ClickType.LEFT) {
                    HistoryRankInventory.nextCurrentPage(player);
                } else if (clickType == ClickType.RIGHT) {
                    HistoryRankInventory.lastCurrentPage(player);
                }
                return;
            }
        }
    }
}
